﻿using DataCollectionVarConfig.Views;
using LPLS.DBEntities.ConfigModels.DeviceInfoModels;
using LPLS.DBInterface.ConfigModels.DeviceInfoModels;
using LPLS.PluginCommon;
using LPLS.PluginCommon.ModulePlugin;
using Prism.Ioc;
using System;
using System.Collections.ObjectModel;
using System.Windows.Controls;

namespace DataCollectionVarConfig.ViewModels
{

    [ViewModelMetadata(projectID: "12322222", viewModelName: "数据采集配置", version: "1.0.1", lifecycle: Lifecycle.Singleton, index: 0, isCommon: true)]
    public class DataCollectionVarConfigViewModel : ViewModelBase
    {
        IDeviceBaseInfoService _deviceBaseInfoService;

        private ObservableCollection<TabItem> _tabItems;

        public DataCollectionVarConfigViewModel(IContainerProvider containerProvider, IDeviceBaseInfoService deviceBaseInfoService) : base(containerProvider)
        {
            
            InitializeTabs();
            _deviceBaseInfoService = deviceBaseInfoService;
            LoadData();
        }

        public ObservableCollection<TabItem> TabItems
        {
            get { return _tabItems; }
            set { SetProperty(ref _tabItems, value); }
        }

        private void InitializeTabs()
        {
            TabItems = new ObservableCollection<TabItem>
        {

              CreateTabItem("PLC属性配置", typeof(DataCollectionPLCVarConfigView)),
               CreateTabItem("PLC规则配置", typeof(DataCollectionPLCRuleEdgeView)),
            
        };
        }




        #region  设备数据加载 条件筛选
        /// <summary>
        /// 
        /// </summary>
        private void LoadData()
        {
         
            DeviceList = new ObservableCollection<DeviceBaseInfo>(_deviceBaseInfoService.GetData());




        }

        private ObservableCollection<DeviceBaseInfo> _DeviceList;

        public ObservableCollection<DeviceBaseInfo> DeviceList
        {
            get { return _DeviceList; }
            set
            {
                SetProperty(ref _DeviceList, value);

            }
        }

        private DeviceBaseInfo _SelectDeviceModel;
        public DeviceBaseInfo SelectDeviceModel
        {
            get { return _SelectDeviceModel; }
            set
            {

                SetProperty(ref _SelectDeviceModel, value);
                if (value != null) {


                    DeviceComponents = new ObservableCollection<DeviceComponentInfo>(value.DeviceComponents);


                }

            }
        }

        private ObservableCollection<DeviceComponentInfo> _DeviceComponents;

        public ObservableCollection<DeviceComponentInfo> DeviceComponents
        {
            get { return _DeviceComponents; }
            set {
                SetProperty(ref _DeviceComponents,value);
                
            }
        }

        private DeviceComponentInfo _SelectComponentModel;

        public DeviceComponentInfo SelectComponentModel
        {
            get { return _SelectComponentModel; }
            set {

                if ( value!=null&&  value != _SelectComponentModel)
                {
                    DataCollectionVarConfigEvent.PublishDataCollectionDeviceChange(new DataCollectionVarConfigEventModel
                    {


                        DeviceComponent = value,
                        DeviceModel = this.SelectDeviceModel


                    });
                }
                SetProperty( ref _SelectComponentModel, value);
              }
        }




        #endregion

















    }
}
